package com.geovis.emergency.spd.biz.filing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.common.core.constant.CommonConstants;
import com.geovis.emergency.spd.biz.filing.mapper.FilingSystemDeptMapper;
import com.geovis.emergency.spd.biz.filing.mapper.FilingSystemUserMapper;
import com.geovis.emergency.spd.biz.filing.service.IFilingSystemDeptService;
import com.geovis.emergency.spd.biz.filing.service.IFilingSystemUserRoleService;
import com.geovis.emergency.spd.biz.filing.service.IFilingSystemUserService;
import com.geovis.emergency.spd.entity.filing.entity.FilingSystemDept;
import com.geovis.emergency.spd.entity.filing.entity.FilingSystemUser;
import com.geovis.emergency.spd.entity.filing.entity.FilingSystemUserRole;
import com.geovis.emergency.spd.entity.system.entity.SystemDept;
import com.geovis.emergency.spd.entity.system.entity.SystemUser;
import com.geovis.emergency.spd.entity.system.entity.SystemUserRole;
import com.geovis.emergency.spd.entity.system.pojo.constant.SystemConstant;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemUserSaveDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
@Slf4j
@DS("filing")
public class FilingSystemUserServiceImpl extends ServiceImpl<FilingSystemUserMapper, FilingSystemUser> implements IFilingSystemUserService {

    private final IFilingSystemUserRoleService filingSystemUserRoleService;

    private final FilingSystemDeptMapper filingSystemDeptMapper;

    private final PasswordEncoder passwordEncoder;


    @Override
    public void saveOrUpdateUser(SystemUserSaveDTO saveDTO, String spdUserName, String deptName, String roleName) {
        String userId=null;
        //如果
        if(null!=spdUserName)
        {
            //用户可能会修改用户名，如果修改了，则预案备案这边的用户相当于新增
            List<FilingSystemUser> filingSystemUser=this.list(Wrappers.lambdaUpdate(FilingSystemUser.class).eq(FilingSystemUser::getUserName,spdUserName));
            if(!CollectionUtils.isEmpty(filingSystemUser))
            {
                userId=filingSystemUser.get(0).getId();
            }

        }
        FilingSystemDept dept=filingSystemDeptMapper.selectOne(Wrappers.lambdaUpdate(FilingSystemDept.class).eq(FilingSystemDept::getDeptName,deptName));
        String roleId=filingSystemDeptMapper.getRoleIdByName(roleName);
        FilingSystemUser user = BeanUtil.toBean(saveDTO, FilingSystemUser.class);
        user.setDeptId(dept.getId());
        user.setDataStatus(CommonConstants.YES);
        user.setUserPassword(passwordEncoder.encode(SystemConstant.defaultPassword));
        user.setId(userId);
        this.saveOrUpdate(user);
        if(userId!=null)
        {
            filingSystemUserRoleService.remove(Wrappers.lambdaUpdate(FilingSystemUserRole.class).eq(FilingSystemUserRole::getUserId, userId));
        }
        FilingSystemUserRole userRole = new FilingSystemUserRole();
        userRole.setRoleId(roleId)
                .setUserId(user.getId());
        filingSystemUserRoleService.save(userRole);
    }

    @Override
    public void removeUsers(List<String> userNames) {

        List<FilingSystemUser> users=this.list(Wrappers.lambdaUpdate(FilingSystemUser.class).in(FilingSystemUser::getUserName, userNames));
        List<String> userIds=users.stream().map(FilingSystemUser::getId).collect(Collectors.toList());
        filingSystemUserRoleService.remove(Wrappers.lambdaUpdate(FilingSystemUserRole.class).in(FilingSystemUserRole::getUserId, userIds));
        this.remove(Wrappers.lambdaUpdate(FilingSystemUser.class).in(FilingSystemUser::getUserName, userNames));

    }
}
